<?php if(!defined('BASEPATH')) exit('No direct script allowed');

class mdl_penjualan extends CI_Model{
	
	function __construct()
	{
		parent::__construct();
	}
	
	function getItem($num=0, $offset=0)
	{
		$this->db->flush_cache();
		$this->db->select('penjualan.id_penjualan, penjualan.diskon,  penjualan.so_no, pelanggan.kode_pelanggan AS kode_pelanggan, pelanggan.nama AS nama_pelanggan, cabang.nama_cabang, penjualan.tanggal, penjualan.jatuh_tempo');
		$this->db->from('penjualan');
		$this->db->join('cabang', 'cabang.id_cabang = penjualan.id_cabang');
		$this->db->join('pelanggan', 'pelanggan.id_pelanggan = penjualan.id_pelanggan');
		$this->db->where('cabang.id_cabang', $this->session->userdata('idcabang'));
		$this->db->limit($num, $offset);
		return $this->db->get();
	}
	
	function getItemById($id)
	{
		$this->db->flush_cache();
		$this->db->where('id_penjualan', $id);
		return $this->db->get('penjualan');
	}

	function insert($data)
	{
		$this->db->flush_cache();
		$this->db->insert('penjualan', $data);
	}
	
	function insert_detail($data)
	{
		$this->db->flush_cache();
		$this->db->insert('detail_penjualan', $data);
	}
	
	function update($id, $data)
	{
		$this->db->flush_cache();
		$this->db->where('id_penjualan', $id);
		$this->db->update('penjualan', $data);
	}
	
	function delete($id)
	{
		$this->db->flush_cache();
		$this->db->delete('penjualan', array('id_penjualan' => $id));
	}
	
	
	function get_detail($id_request)
	{
		$this->db->flush_cache();
		$this->db->select('detail_penjualan.*, barang.*');
		$this->db->from('detail_penjualan');
		$this->db->join('barang', 'barang.id_barang = detail_penjualan.id_barang');
		$this->db->where('id_penjualan', $id_request);
		return $this->db->get();
	}
	
	# untuk meng-generate nomer PO
	function get_so()
	{
		# ambil code user
		$code_user = get_userid();
		$code_user = str_pad($code_user, 3, '0', STR_PAD_LEFT);
		
		# ambil kode cabang
		$this->db->flush_cache();
		$this->db->select('users.*, cabang.*, karyawan.*');
		$this->db->from('users');
		$this->db->join('karyawan', 'karyawan.userid = users.userid');
		$this->db->join('cabang', 'cabang.id_cabang = karyawan.id_cabang');
		$this->db->where('users.userid', get_userid());
		$kode_cabang = $this->db->get()->row()->kode_cabang;
		
		# ambil tahun bulan dan tanggal
		$tanggal = date('ymd');
		
		# ambil nomer running berdasarkan
		$this->db->flush_cache();
		$this->db->from('penjualan');
		$this->db->like('so_no', 'SO-'.$kode_cabang.$code_user, 'after');
		$query = $this->db->get();
		
		$no_so = $query->num_rows();
		$no_so = (int) $no_so + 1;
		$no_so = str_pad($no_so, 6, '0', STR_PAD_LEFT);
		
		return 'SO-'.$kode_cabang.$code_user.$tanggal.$no_so;
		
	}
	
	# untuk di list barang pada fancyBox
	function get_barang()
	{
		$this->db->flush_cache();
		$this->db->select('pembelian.id_pembelian, detail_pembelian.id_barang, detail_pembelian.id_detail_pembelian, jenis.*, kategori.*, barang.nama_barang, detail_pembelian.harga, detail_pembelian.sn, barang.is_hargatoko, barang.is_hargapartai, barang.is_hargajual');
		$this->db->from('pembelian');
		$this->db->join('detail_pembelian', 'detail_pembelian.id_pembelian = pembelian.id_pembelian');
		$this->db->join('barang', 'barang.id_barang = detail_pembelian.id_barang');
		$this->db->join('jenis', 'jenis.id_jenis = barang.id_jenis');
		$this->db->join('kategori', 'kategori.id_kategori = barang.id_kategori');
		$this->db->group_by('detail_pembelian.id_barang');
		$this->db->order_by('detail_pembelian.id_detail_pembelian', 'ASC');
		$this->db->where('pembelian.id_cabang', get_idcabang());
		return $this->db->get();
	}
	
	# untuk di items yg akan di ambil
	function get_items($id_pembelian, $id_barang){
		$this->db->flush_cache();
		$this->db->select('pembelian.id_pembelian, detail_pembelian.id_barang, detail_pembelian.id_detail_pembelian, jenis.*, kategori.*, barang.nama_barang, detail_pembelian.harga, detail_pembelian.sn, barang.is_hargatoko, barang.is_hargapartai, barang.is_hargajual, barang.harga_cabang, barang.harga_toko, barang.harga_partai');
		$this->db->from('pembelian');
		$this->db->join('detail_pembelian', 'detail_pembelian.id_pembelian = pembelian.id_pembelian');
		$this->db->join('barang', 'barang.id_barang = detail_pembelian.id_barang');
		$this->db->join('jenis', 'jenis.id_jenis = barang.id_jenis');
		$this->db->join('kategori', 'kategori.id_kategori = barang.id_kategori');
		//$this->db->group_by('detail_pembelian.id_barang');
		$this->db->order_by('detail_pembelian.id_detail_pembelian', 'ASC');
		$this->db->where('detail_pembelian.id_pembelian', $id_pembelian);
		$this->db->where('detail_pembelian.id_barang', $id_barang);
		$this->db->where('detail_pembelian.soldout != ', '1');
		return $this->db->get();
	}
	
	function updateBarangPembelian($id_detailpembelian, $nilai){
		$this->db->flush_cache();
		$this->db->where('id_detail_pembelian', $id_detailpembelian);
		$this->db->update('detail_pembelian', array('soldout'=> $nilai));
	}
	
}